package com.qifenqian.turbot.module.ui.loginetc

import android.content.Context
import android.content.Intent
import android.view.View
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.config.Constant
import com.qifenqian.turbot.config.TurbotEnum
import com.qifenqian.turbot.module.contract.AccountContract
import com.qifenqian.turbot.module.entity.response.LoginEntity
import com.qifenqian.turbot.module.entity.response.VersionEntity
import com.qifenqian.turbot.module.presenter.AccountPresenter
import com.qifenqian.turbot.module.ui.MainActivity
import com.qifenqian.turbot.util.SendSmsUtil
import com.qifenqian.turbot.util.SpUtils
import com.qifenqian.turbot.util.StringUtils
import com.qifenqian.turbot.util.TurbotUtil
import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.toast

/**
 * @author H
 * @createDate 2018/12/12 16:17
 * @description 登录
 * @see
 * @since
 */
const val VERSION_ENTITY = "versionEntity"

class LoginActivity : BaseActivity<AccountPresenter, AccountContract.View>(), AccountContract.View {

    companion object {
        fun startThis(context: Context, versionEntity: VersionEntity? = null) {
            val intent = Intent(context, LoginActivity::class.java)
            intent.putExtra(VERSION_ENTITY, versionEntity)
            context.startActivity(intent)
        }
    }

    private var mLoginType: TurbotEnum = TurbotEnum.LOGIN_TYPE_PASSWORD  //登录方式

    override fun bindLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun bindPresenter(): AccountPresenter? {
        return AccountPresenter(this, this)
    }

    private var mVersionEntity: VersionEntity? = null

    override fun initial() {
        mVersionEntity = intent.getParcelableExtra(VERSION_ENTITY)

        if (mVersionEntity != null && !TurbotUtil.getCurrentVersion(this).isEmpty()) {
            if (mVersionEntity!!.version != TurbotUtil.getCurrentVersion(this)) {
                showUpdateDialog()
            }
        }

        mLoginType = TurbotEnum.LOGIN_TYPE_SMS //布局中设置了默认显示密码登录布局
        btn_login_way.setOnClickListener {
            when (mLoginType) {
                TurbotEnum.LOGIN_TYPE_PASSWORD -> {
                    et_login_password.visibility = View.VISIBLE
                    ly_login_sms.visibility = View.GONE
                    btn_login_way.text = "验证码登录"
                    mLoginType = TurbotEnum.LOGIN_TYPE_SMS
                }
                TurbotEnum.LOGIN_TYPE_SMS      -> {
                    et_login_password.visibility = View.GONE
                    ly_login_sms.visibility = View.VISIBLE
                    btn_login_way.text = "密码登录"
                    mLoginType = TurbotEnum.LOGIN_TYPE_PASSWORD
                }
            }
        }
        btn_login_get_code.setOnClickListener {
            if (et_login_account.text.toString().isBlank()) {
                toast("请输入手机号")
                return@setOnClickListener
            }
            if (!StringUtils.isPhoneNumber(et_login_account.text.toString())) {
                toast("手机号码格式错误")
                return@setOnClickListener
            }
            //发送验证码
            mPresenter!!.sendSmsCode(TurbotEnum.SMS_LOGIN.type)
        }
        btn_login_login.setOnClickListener {
            if (!isNetworkConnected()) return@setOnClickListener
            if (et_login_account.text.toString().isBlank()) {
                toast("请输入账号")
                return@setOnClickListener
            }
            if (mLoginType == TurbotEnum.LOGIN_TYPE_PASSWORD) {
                if (et_login_sms.text.toString().isBlank()) {
                    toast("请输入验证码")
                    return@setOnClickListener
                }
                mPresenter!!.login(et_login_account.text.toString(), loginType = TurbotEnum.LOGIN_TYPE_SMS.type, smsCode = et_login_sms.text.toString(), showDialog = true)
            } else if (mLoginType == TurbotEnum.LOGIN_TYPE_SMS) {
                if (et_login_password.text.toString().isBlank()) {
                    toast("请输入密码")
                    return@setOnClickListener
                }
                mPresenter!!.login(et_login_account.text.toString(), password = et_login_password.text.toString(), loginType = TurbotEnum.LOGIN_TYPE_PASSWORD.type, showDialog = true)
            }
        }
        btn_login_register.setOnClickListener {
            RegisterActivity.startThis(this)
        }
        btn_login_forget.setOnClickListener {
            ForgetPasswordActivity.startThis(this)
        }
    }

    private fun showUpdateDialog() {
        //更新的dialog
    }

    override fun sendSmsCodeResult(isSuccess: Boolean, code: Int, msg: String?) {
        if (isSuccess) {
            //开启倒计时
            SendSmsUtil.beginCountdown(this, Constant.COUNTDOWN_TIME, btn_login_get_code)
            toast("发送成功")
        } else {
            //请求验证码失败则关闭倒计时
            SendSmsUtil.disposeCountdown()
//        btn_login_get_code.text = "重新发送"
//        btn_login_get_code.setTextColor(Color.parseColor("#ff0080ff"))
//        btn_login_get_code.isClickable = true
            toast(msg!!)
        }
    }

    override fun loginResult(isSuccess: Boolean, code: Int, msg: String?, loginEntity: LoginEntity?, versionEntity: VersionEntity?, password: String?) {
        if (isSuccess) {
            SpUtils.getInstance().putString(Constant.SP_KEY_PASSWORD, password!!, Constant.SP_ACCOUNT)
            SpUtils.getInstance().putInt(Constant.SP_KEY_RESTAURANT_ID, loginEntity!!.restaurantId, Constant.SP_ACCOUNT)
            SpUtils.getInstance().putString(Constant.SP_KEY_USER_CODE, loginEntity.userCode, Constant.SP_ACCOUNT)
            MainActivity.startThis(this, null)
            finish()
        } else {
            toast(msg!!)
        }
    }
}
